package telas.componentes;

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;

import business.util.Mascaras;
import business.util.Menssagens;

public class CampoData extends CampoEditavel
{
	private static final long serialVersionUID = -5614393357304960822L;

	/**
	 * This method initializes 
	 * 
	 */
	public CampoData() throws Exception
	{
		super(Mascaras.Data());
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() 
	{
        this.setSize(new Dimension(129, 23));
	}
	
	@Override
	public void setText(String arg0) 
	{
		super.setText(arg0);
	}
	
	public void setData(Date date) throws Exception
	{
		SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
		
		try
		{			
			setText(formato.format(date));
		}
		catch(Exception ex)
		{
			throw new Exception("Data " + Menssagens.itemInvalido);
		}
	}
	
	public Date getData() throws Exception
	{
		SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
		String dataTexto = getText();		
		Date data = null;
		
		try
		{
			data = formato.parse(dataTexto);
		}
		catch(Exception ex)
		{
			throw new Exception("Data " + Menssagens.itemInvalido);
		}
		
		return data;
	}
}
